package com.csun.cmny.provider.controller;


import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.dto.OmcMonitorLogDto;
import com.csun.cmny.provider.dto.OmcMonitorLogQueryDto;
import com.csun.cmny.provider.model.vo.OmcUserVo;
import com.csun.cmny.provider.pojo.OmcMonitorLog;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcMonitorLogService;
import com.csun.cmny.provider.validator.OmcMonitorLogValidator;
import com.csun.cmny.provider.vo.OldManMVo;
import com.csun.cmny.provider.vo.OmcMonitorLogVo;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.regex.Pattern;

@RestController
@RequestMapping(value = "/monitorlog", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcMonitorLogController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcMonitorLogService omcMonitorLogService;

    /**
     * 添加班长日志
     */
    @ValidateAnnotation
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/mLogAdd", method = RequestMethod.POST)
    public Wrapper mLogAdd(@RequestBody @Validated(value = {OmcMonitorLogValidator.class}) OmcMonitorLogDto monitorLogDto, BindingResult br) {
        monitorLogDto.setGroupId(getLoginAuthDto().getGroupId());
        int m = saveOmcMonitorLog(monitorLogDto);//添加班长日志
        if (m <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("comment.add.fail", null), m);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.add.success", null), m);
    }


    /**
     * 修改班长日志
     */
    @ValidateAnnotation
    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public Wrapper updateById(@RequestBody @Validated(value = {OmcMonitorLogValidator.class}) OmcMonitorLogDto monitorLogDto, BindingResult br) {
        if (null == monitorLogDto.getId() || monitorLogDto.getId() == 0) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("Monitorlog.id.null", null), null);
        }
        monitorLogDto.setGroupId(getLoginAuthDto().getGroupId());
        int m = saveOmcMonitorLog(monitorLogDto);//修改班长日志
        if (m <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("comment.update.fail", null), m);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.update.success", null), m);
    }

    /**
     * @param: [planId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/mLogDelById", method = RequestMethod.POST)
    public Wrapper mLogDelById(Long id) {

        // 校验Id是否为空
        Preconditions.checkArgument(id != null, cmnyMessageSource
                .getMessage("Monitorlog.id.null", null));
        Integer result = omcMonitorLogService.deleteByKey(id);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("comment.delete.fail", null));
        }
        return WrapMapper.ok(result);
    }

    /**
     * 管理员查看班长日志
     */
    @RequestMapping(value = "/selectShowAlls", method = RequestMethod.POST)
    public Wrapper selectShowAlls(@RequestBody OmcMonitorLogQueryDto omcMonitorLogQueryDto) {
        String pattern = "^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])"
                + "|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|"
                + "[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$";

        //开始时间
        if (StringUtils.isNotBlank(omcMonitorLogQueryDto.getDate()) && !Pattern.matches(pattern, omcMonitorLogQueryDto.getDate())) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("mrc.date.format", null), null);
        }
        //班组ID
        if (null == omcMonitorLogQueryDto.getNursingGroupId() || 0 == omcMonitorLogQueryDto.getNursingGroupId()) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("group.id.error", null), null);
        }
        omcMonitorLogQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        PageHelper.startPage(omcMonitorLogQueryDto.getPageNum(), omcMonitorLogQueryDto.getPageSize());
        List<OmcMonitorLogVo> list = omcMonitorLogService.queryMonitorLogList(omcMonitorLogQueryDto);
        PageInfo<OmcMonitorLogVo> pageInfo = new PageInfo<>(list);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.query.success", null), pageInfo);
    }

    /**
     * 查看班长日志
     */
    @RequestMapping(value = "/selectShowAll", method = RequestMethod.POST)
    public Wrapper selectShowAll(@RequestBody OmcMonitorLogQueryDto omcMonitorLogQueryDto) {
        String pattern = "^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])"
                + "|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|"
                + "[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$";

        //开始时间
        if (StringUtils.isNotBlank(omcMonitorLogQueryDto.getDate()) && !Pattern.matches(pattern, omcMonitorLogQueryDto.getDate())) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("mrc.date.format", null), null);
        }
        omcMonitorLogQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        PageHelper.startPage(omcMonitorLogQueryDto.getPageNum(), omcMonitorLogQueryDto.getPageSize());
        List<OmcMonitorLogVo> list = omcMonitorLogService.queryMonitorLogList(omcMonitorLogQueryDto);
        PageInfo<OmcMonitorLogVo> pageInfo = new PageInfo<>(list);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.query.success", null), pageInfo);
    }

    /**
     * 通过id查询班长日志
     */
    @RequestMapping(value = "/selectById", method = RequestMethod.POST)
    public Wrapper selectById(@RequestBody OmcMonitorLogQueryDto omcMonitorLogQueryDto) {

        if (null == omcMonitorLogQueryDto.getId() || 0 == omcMonitorLogQueryDto.getId()) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("Monitorlog.id.null", null), null);
        }
        omcMonitorLogQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        List<OmcMonitorLogVo> list = omcMonitorLogService.queryMonitorLogList(omcMonitorLogQueryDto);
        if (null == list || list.size() <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("comment.query.fail", null), null);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.query.success", null), list);
    }


    /**
     * 模糊查询老人
     */
    @RequestMapping(value = "/selectByName", method = RequestMethod.POST)
    public Wrapper selectByName(@RequestBody OmcMonitorLogQueryDto omcMonitorLogQueryDto) {
        omcMonitorLogQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        //班组ID
        if (null == omcMonitorLogQueryDto.getNursingGroupId() || 0 == omcMonitorLogQueryDto.getNursingGroupId()) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("group.id.error", null), null);
        }
        //查询全部
        omcMonitorLogQueryDto.setPageNum(0);
        omcMonitorLogQueryDto.setPageSize(0);
        List<OldManMVo> list = omcMonitorLogService.selectOldmanBynameM(omcMonitorLogQueryDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.query.success", null), list);
    }


    /**
     * 模糊查询负责人
     */
    @RequestMapping(value = "/selectUserByName", method = RequestMethod.POST)
    public Wrapper selectUserByName(@RequestBody OmcMonitorLogQueryDto omcMonitorLogQueryDto) {
        omcMonitorLogQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        //班组ID
        if (null == omcMonitorLogQueryDto.getNursingGroupId() || 0 == omcMonitorLogQueryDto.getNursingGroupId()) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("group.id.error", null), null);
        }
        //查询全部
        omcMonitorLogQueryDto.setPageNum(0);
        omcMonitorLogQueryDto.setPageSize(0);
        List<OmcUserVo> list = omcMonitorLogService.selectUserByName(omcMonitorLogQueryDto);
        if (null == list || list.size() <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("comment.query.fail", null), null);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.query.success", null), list);
    }

    /**
     * 保存班长日志
     *
     * @param: [uacPlanDto]
     * @return: java.lang.Integer
     */
    private Integer saveOmcMonitorLog(OmcMonitorLogDto omcMonitorLogDto) {
        OmcMonitorLog omcMonitorLog = new OmcMonitorLog();
        BeanUtils.copyProperties(omcMonitorLogDto, omcMonitorLog);
        return omcMonitorLogService.saveMonitorLog(omcMonitorLog, getLoginAuthDto());
    }


}
